import request from '../../../utils/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navbar: ['待付款', '待发货', '已发货', '售后服务'],
    orderStatus: 0,
    orderListInfo: [],
    height: 0
  },

  navbarTap(options) {
    var options = {
      orderStatus: options.currentTarget.dataset.idx
    }
    this.onLoad(options)
  },

  swiperChange(options) {
    var options = {
      orderStatus: options.detail.current
    }
    this.onLoad(options)
  },

  /**
   * 调整swiper高度
   */
  setSwiperHeight(orderStatus) {
    var that = this;
    let height = this.data.height;
    if (orderStatus >= 0) {
      //创建节点选择器
      var query = wx.createSelectorQuery();
      //选择id
      query.select('#ordetstatus' + orderStatus).boundingClientRect()
      query.exec(function(res) {
        //res就是 所有标签为mjltest的元素的信息 的数组
        // console.log(res);
        //取高度
        height = res[0].height;
        that.setData({
          height
        })
      })
      return;
    }
    that.setData({
      height: 550
    })
    return;
  },

  /**
   * 取消订单按钮
   */
  async cancelOrder(options) {
    var that = this
    let orderId = options.currentTarget.dataset.id;
    let result = await request("/order/cancel", {
      orderId
    });
    if (result.code == 200) {
      let options = {
        orderStatus: 3
      }
      this.onLoad(options)
      wx.showToast({
        title: '取消订单成功！'
      })
    }
  },

  toOrderDetail(options) {
    let orderId = options.currentTarget.dataset.id
    wx.navigateTo({
      url: '/orderPackage/pages/orderDetail/orderDetail?orderId=' + orderId,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function(options) {
    let orderStatus = options.orderStatus;
    let orderListResult = await request("/order/list", {
      orderStatus
    });

    this.setData({
      orderStatus,
      orderListInfo: orderListResult.data
    })
    this.setSwiperHeight(orderListResult.data != null ? orderStatus : -1)


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: async function(options) {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})